home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBGRX100.ARJ / TEXTSIZE.C < prev    next >
Text File  |  1992-04-10  |  4KB  |  155 lines

  1. /** 
  2.  ** TEXTSIZE.C 
  3.  **
  4.  **  Copyright (C) 1992, Csaba Biegl
  5.  **    820 Stirrup Dr, Nashville, TN, 37221
  6.  **    csaba@vuse.vanderbilt.edu
  7.  **
  8.  **  This file is distributed under the terms listed in the document
  9.  **  "copying.cb", available from the author at the address above.
  10.  **  A copy of "copying.cb" should accompany this file; if not, a copy
  11.  **  should be available from where this file was obtained.  This file
  12.  **  may not be distributed without a verbatim copy of "copying.cb".
  13.  **  You should also have received a copy of the GNU General Public
  14.  **  License along with this program (it is in the file "copying");
  15.  **  if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16.  **  Cambridge, MA 02139, USA.
  17.  **
  18.  **  This program is distributed in the hope that it will be useful,
  19.  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  **  GNU General Public License for more details.
  22.  **/
  23.  
  24. #include "grx.h"
  25. #include "libgrx.h"
  26. #include "grxfont.h"
  27.  
  28. GrFont *_GrDefaultFont = NULL;
  29.  
  30. GrFont *_GrGetDefaultFont(void)
  31. {
  32.     switch(_GrAdapterType) {
  33.       case GR_EGA:
  34.         return(_GrDefaultFont = GrLoadBIOSFont("@:pc8x14.fnt"));
  35.       case GR_VGA:
  36.         return(_GrDefaultFont = GrLoadBIOSFont("@:pc8x16.fnt"));
  37.       default:
  38.         return(NULL);
  39.     }
  40. }
  41.  
  42. int _GrGetTextSize(char *txt,int len,int *w,int *h,GrTextOption *opt)
  43. {
  44.     GrFont *font = CHECK_FONT(opt->txo_font);
  45.     int ww,hh,chr;
  46.  
  47.     if((font == NULL) || (opt->txo_xmag <= 0) || (opt->txo_ymag <= 0)) {
  48.         *w = 0;
  49.         *h = 0;
  50.         return(FALSE);
  51.     }
  52.     if(font->fnt_isfixed) {
  53.         ww = font->fnt_width  * opt->txo_xmag * len;
  54.         hh = font->fnt_height * opt->txo_ymag;
  55.     }
  56.     else {
  57.         for(ww = 0; --len >= 0; ) {
  58.         switch(opt->txo_chrtype) {
  59.           case GR_WORD_TEXT:
  60.             chr = *((unsigned short *)txt)++;
  61.             break;
  62.           case GR_ATTR_TEXT:
  63.             chr = *((short *)txt)++ & 0xff;
  64.             break;
  65.           default:
  66.             chr = *((unsigned char *)txt)++;
  67.             break;
  68.         }
  69.         if(chr > font->fnt_maxchar) continue;
  70.         if((chr -= font->fnt_minchar) < 0) continue;
  71.         ww += PFP(font)->pf_width[chr];
  72.         }
  73.         ww *= opt->txo_xmag;
  74.         hh = font->fnt_height * opt->txo_ymag;
  75.     }
  76.     switch(opt->txo_direct) {
  77.       case GR_TEXT_DOWN:
  78.         *w = (-hh);
  79.         *h = ww;
  80.         break;
  81.       case GR_TEXT_LEFT:
  82.         *w = (-ww);
  83.         *h = (-hh);
  84.         break;
  85.       case GR_TEXT_UP:
  86.         *w = hh;
  87.         *h = (-ww);
  88.         break;
  89.       default:
  90.         *w = ww;
  91.         *h = hh;
  92.     }
  93.     return(TRUE);
  94. }
  95.  
  96. int GrStringWidth(char *text,int length,GrTextOption *opt)
  97. {
  98.     int ww,hh;
  99.  
  100.     _GrGetTextSize(text,length,&ww,&hh,opt);
  101.     return(IABS(ww));
  102. }
  103.  
  104. int GrStringHeight(char *text,int length,GrTextOption *opt)
  105. {
  106.     int ww,hh;
  107.  
  108.     _GrGetTextSize(text,length,&ww,&hh,opt);
  109.     return(IABS(hh));
  110. }
  111.  
  112. int GrCharWidth(int chr,GrTextOption *opt)
  113. {
  114.     char buff[1];
  115.  
  116.     buff[0] = chr;
  117.     return(GrStringWidth(buff,1,opt));
  118. }
  119.  
  120. int GrCharHeight(int chr,GrTextOption *opt)
  121. {
  122.     char buff[1];
  123.  
  124.     buff[0] = chr;
  125.     return(GrStringHeight(buff,1,opt));
  126. }
  127.  
  128. static int fontsize(GrTextOption *opt,int which)
  129. {
  130.     GrFont *font = CHECK_FONT(opt->txo_font);
  131.     int w,h;
  132.  
  133.     if(font == NULL) return(0);
  134.     w = opt->txo_xmag * font->fnt_width;
  135.     h = opt->txo_ymag * font->fnt_height;
  136.     switch(opt->txo_direct) {
  137.       case GR_TEXT_DOWN:
  138.       case GR_TEXT_UP:
  139.         return(which ? w : h);
  140.       default:
  141.         return(which ? h : w);
  142.     }
  143. }
  144.  
  145. int GrFontWidth(GrTextOption *opt)
  146. {
  147.     return(fontsize(opt,0));
  148. }
  149.  
  150. int GrFontHeight(GrTextOption *opt)
  151. {
  152.     return(fontsize(opt,1));
  153. }
  154.  
  155.